package com.wlys.player.controler.player.engine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Handler;
import com.wlys.player.controler.player.interf.IPlayerEngine;
import com.wlys.player.controler.player.interf.OnPlayerState;
import com.wlys.player.model.Track;

public class PlayerEngine implements IPlayerEngine
{

    Context mContext;
    MediaPlayer mMediaPlayer;
    Track mTrack;
    // OnPlayerState mPlayerState;
    List<OnPlayerState> mOnPlayerStateList;
    OnErrorListener mOnErrorListener = new OnErrorListener()
    {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra)
        {
            // TODO Auto-generated method stub
            return false;
        }
    };
    OnPreparedListener mOnPreparedListener = new OnPreparedListener()
    {
        @Override
        public void onPrepared(MediaPlayer mp)
        {
            for (OnPlayerState onPlayerState : mOnPlayerStateList)
            {
                onPlayerState.maxSize(mMediaPlayer.getDuration());
            }

            mMediaPlayer.start();
            mUiHandler.post(mRunnable);
        }
    };
    OnSeekCompleteListener mOnSeekCompleteListener = new OnSeekCompleteListener()
    {
        @Override
        public void onSeekComplete(MediaPlayer mp)
        {
            // TODO Auto-generated method stub

        }
    };
    OnCompletionListener mOnCompletionListener = new OnCompletionListener()
    {
        @Override
        public void onCompletion(MediaPlayer mp)
        {
            for (OnPlayerState onPlayerState : mOnPlayerStateList)
            {
                onPlayerState.complete();
            }
        }
    };
    Handler mUiHandler = new Handler()
    {
    };
    Runnable mRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            for (OnPlayerState onPlayerState : mOnPlayerStateList)
            {
                onPlayerState.seekTo(mMediaPlayer.getCurrentPosition());
            }
            mUiHandler.postDelayed(this, 50);
        }
    };

    public PlayerEngine(Context mContext)
    {
        super();
        this.mContext = mContext;
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(mOnErrorListener);
        mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
        mMediaPlayer.setOnSeekCompleteListener(mOnSeekCompleteListener);
        mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
        if (null == mOnPlayerStateList)
        {
            mOnPlayerStateList = new ArrayList<OnPlayerState>();
        }
    }

    @Override
    public void pre(Track track)
    {
        if (null == mMediaPlayer)
        {
            return;
        }
        mMediaPlayer.stop();
        play(track);
    }

    @Override
    public void next(Track track)
    {
        if (null == mMediaPlayer)
        {
            return;
        }
        mMediaPlayer.stop();
        play(track);
    }

    @Override
    public void play(Track track)
    {
        if (null == mMediaPlayer)
        {
            return;
        }
        for (OnPlayerState onPlayerState : mOnPlayerStateList)
        {
            onPlayerState.start();
        }
        mMediaPlayer.reset();
        try
        {
            mMediaPlayer.setDataSource(track.getPath());
        }
        catch (IllegalArgumentException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalStateException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mMediaPlayer.prepareAsync();
    }

    @Override
    public void contin()
    {
        if (null == mMediaPlayer)
        {
            return;
        }
        mMediaPlayer.start();
    }

    @Override
    public void pause()
    {
        if (null == mMediaPlayer)
        {
            return;
        }
        mMediaPlayer.pause();
    }

    @Override
    public void stop()
    {
        if (null == mMediaPlayer)
        {
            return;
        }
        mMediaPlayer.stop();
        mMediaPlayer.release();
        mUiHandler.removeCallbacks(mRunnable);
    }

    @Override
    public void seekTo(int time)
    {
        if (null == mMediaPlayer)
        {
            return;
        }
        mMediaPlayer.seekTo(time);
    }
    
    @Override
    public boolean isPlaying()
    {
        if (null == mMediaPlayer)
        {
            return false;
        }
        return mMediaPlayer.isPlaying();
    }

    public void registOnPlayerState(OnPlayerState onPlayerState)
    {
        if (null != mOnPlayerStateList)
        {
            mOnPlayerStateList.add(onPlayerState);
        }
    }

    public void removeOnPlayerState(OnPlayerState onPlayerState)
    {
        if (null != mOnPlayerStateList)
        {
            mOnPlayerStateList.remove(onPlayerState);
        }
    }
}
